<?php
	require 'config.php';
	require 'init.php';
	/*
	 *  function
	 *  
	 * */
	function cleanStr($strArray){
		if(is_array($strArray)){
			foreach ($strArray as &$val) {
				$val = str_replace("'",'',$val);
				$val = trim($val);
			}
		} else {
			$strArray = trim($strArray);
			$strArray = str_replace("'",'',$strArray);
		}
	
		return $strArray;
	}
	/**/
	function converttoArray($str){
		$matches = array();
		foreach ($str as $s) {
			$match = explode(',',$s);
			$matches[cleanStr($match[2])] = cleanStr($match);
		}
	
		return $matches;
	}
	/**/
	function getInfoMatch($infoMatchString){
		$infoMatchString = str_replace(" ",'',$infoMatchString);
		$infoMatchString = str_replace("'",'',$infoMatchString);
		$infoMatchString = str_replace(");",'',$infoMatchString);
		$infoMatchString = str_replace("bettingESInfo(",'',$infoMatchString);
		return explode(',',$infoMatchString);
	}
	// init category matche
	$cates = CTable('category_match')->select_all();
	$cateMatch = array();
	foreach ($cates as $cate) {
		$cateMatch[$cate['name']] = $cate['id'];
	}
	/*
	 * end function
	 * 
	 * */
	//----------------------------------process-------------------------------------
// 	[pvbcoSEB] => Array
// 	(
// 			[0] => 2300
// 			[1] => JAPAN Emperors Cup
// 			[2] => pvbcoSEB
// 			[3] => 1325131200
// 			[4] => C-Osaka
// 			[5] => FC Tokyo
// 			[6] => 2.27
// 			[7] => 3.35
// 			[8] => 2.96
// 			[9] => 0
// 			[10] => 1.74
// 			[11] => 2.03
// 			[12] => 0
// 			[13] => 1
// 			[14] => 50
// 			[15] => 2
// 	)
	$win29 = file_get_contents('http://win29.com/betting.es.php?page=results');
	preg_match_all("/addESR\((.*)\);/", $win29, $out);
	$matches = converttoArray($out[1]);
	// begin
	$log = new CLogData();
	$log->addlog(date('H:i:s Y/m/d').'update rate:'.count($matches),'result.log');
	//$log->addlog(print_r($matches,1),'result.log');
	foreach ($matches as $match) {
		//if tran dau qua lau
		if ((time() - $match[3]) > 30*60) {
			$log->addlog('no update:'.$match['2'].'-'.(time() - $match[3]),'result.log');
			//continue;
		}
		//update tỷ số
		if($match[15]==1){
			$march_time_end = 0;
			$match_finish = 0;
		}else{
			$march_time_end = time();
			$match_finish = 1;
		}
		$idrate = CTable('match')->update(array(
				'team1_goal' => $match[12],
				'team2_goal' => $match[13],
				'match_end' => $march_time_end,
				'match_finish' => $match_finish
			
		),array('code'=>$match['2']));
		$log->addlog(date('H:i:s Y/m/d').'update:'.$match['2'],'result.log');
		echo 'update:'.$match['2'].'<br>';
		
	}

